﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version:  2.0.50727.4927
 *  NameSpace: Summer.Core.Aop
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009/10/27 16:37:20
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Core.Aop
{
    public abstract class AbstractAOPProperty : IContextProperty, IContributeServerContextSink
    {
        private string m_aspectXml;
        private const string m_name="AOP";

        public AbstractAOPProperty()
        {
            m_aspectXml = string.Empty;
        }
        public string AspectXml
        {
            set 
            {
                m_aspectXml = value;
            }
        }
        /// <summary>
        /// Create new instance of aspect
        /// </summary>
        /// <param name="nextSink">messageSink to receive</param>
        /// <returns></returns>
        protected abstract IMessageSink CreateAspect(IMessageSink nextSink);

        protected virtual string GetName()
        {
            return m_name;
        }
        protected virtual void FreezeImpl(Context newContext)
        {
            return;
        }
        protected virtual bool CheckNewContext(Context newCtx)
        {
            return true;
        }

        #region IContextProperty 成员

        void IContextProperty.Freeze(Context newContext)
        {
            this.FreezeImpl(newContext);
        }

        bool IContextProperty.IsNewContextOK(Context newCtx)
        {
            return this.CheckNewContext(newCtx);
        }

        string IContextProperty.Name
        {
            get { return this.GetName(); }
        }

        #endregion

        #region IContributeServerContextSink 成员

        IMessageSink IContributeServerContextSink.GetServerContextSink(IMessageSink nextSink)
        {
            AbstractAspect aspect = (AbstractAspect)CreateAspect(nextSink);
            aspect.ReadAspect(m_aspectXml, GetName());
            return (IMessageSink)aspect;
        }

        #endregion
    }
}